import sys
import os

"""
将代码转为 容易复制到终端的

"""
def convert_python_file(input_file, output_file):
    with open(input_file, 'r') as f:
        lines = f.readlines()

    converted_lines = []
    last_indent = ''

    for line in lines:
        stripped_line = line.strip()
        if len(stripped_line) > 0 and line[0] == stripped_line[0]:
            if last_indent:
                converted_lines.append('\n')
            last_indent = None

        if stripped_line:
            # 非空行
            indent = line[:len(line) - len(line.lstrip())]
            converted_lines.append(line)
            if indent:
                last_indent = indent
        else:
            # 空行
            if last_indent:
                converted_lines.append(f"{last_indent}#\n")
            else:
                converted_lines.append(line)

    # 写入转换后的内容到输出文件
    with open(output_file, 'w') as f:
        f.writelines(converted_lines)

def main():
    if len(sys.argv) != 2:
        print("Usage: python conver_py.py <input_file_path>")
        sys.exit(1)

    input_file = sys.argv[1]
    if not os.path.isfile(input_file):
        print(f"Error: The file '{input_file}' does not exist.")
        sys.exit(1)

    # 获取输入文件的目录、文件名和扩展名
    file_dir, file_name = os.path.split(input_file)
    file_name_without_ext, file_ext = os.path.splitext(file_name)

    # 创建输出文件路径
    output_file = os.path.join(file_dir, f"{file_name_without_ext}-format{file_ext}")

    convert_python_file(input_file, output_file)
    print(f"转换完成 Conversion complete. Output file 输出的路径: {output_file}")



if __name__ == "__main__":
    main()


#
